<template>
  <div id="header">
    <!--      顶部左边-->
    <div class="left">
      <!--        logo-->
      <div @click="goHome" class="logo"></div>
      <div class="welcome">欢迎您，{{this.$store.getters.name}}&nbsp;同学</div>
    </div>

    <!--      顶部右边-->
    <div class="right">
      <div>
        <el-dropdown>
          <div class="me" style="width: 50px;height: 50px;margin-top: 5px">
            <el-image fit="cover" :src="avatarUrl" style="border-radius: 10px;height: 100%;width: 100%"></el-image>
          </div>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="my">个人中心</el-dropdown-item>
            <el-dropdown-item @click.native="logout">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </div>
  </div>
</template>

<script>
import {logout} from "@/api/login"
export default {
  data(){
    return{
      avatarUrl: '',
    }
  },
  async created(){
    await this.$store.dispatch("GetLoginInfo");
    this.avatarUrl = "http://localhost:8080" +  this.$store.getters.avatar
  },
  methods:{
    logout(){
      // 清除登录状态，返回登录页
      console.log(11);
      
      this.$store.dispatch("LoginOut").then(()=> {
        logout().then(res => {
          this.$message(res.msg)
        })
        
      }).catch(()=>{})
    },
    // 返回首页
    goHome(){
      if(this.$route.path !== '/'){
        this.$router.push("/")
      }
    },

    // 个人中心
    my(){
      this.$router.push("/my")
    }
  }
}
</script>

<style scoped>
#header {
  background-color: aqua;
  display: flex;
  justify-content: space-between;
  line-height: 70px;
  color: white;
}
.left {
  display: flex;
}
.right {
  width: 100px;
}
.logo {
  background-image: url("../assets/images/logo.png");
  background-position: center;
  background-size: cover;
  height: 70px;
  width: 200px;
}
.me:hover{
  color: white;
  cursor: pointer;
}
</style>
